package com.sync;

public class DisSync3 implements Runnable {

    static int i = 0;
    static DisSync3 dis = new DisSync3();

    public static void main(String[] args)throws Exception {
        Thread t1 = new Thread(dis,"t1");
        Thread t2 = new Thread(dis,"t2");
        t1.start();
        t2.start();
        // 通过join保证两个线程都执行完成
        t1.join();
        t2.join();

        System.out.println(i);
    }

    @Override
    public void run() {
        synchronized (DisSync3.class){
            System.out.println(Thread.currentThread()+"开始执行");
            for (int j = 0; j < 100000; j++) {
                i ++;
            }
            System.out.println(Thread.currentThread()+"执行完毕");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }
        }
    }
}
